<template>
  <div id="commodity">
    <!-- 左侧菜单导航栏 -->
    <div class="menu-wrapper">
      <ul class="menu" id="menu">
        <li
          v-for="(menuItem, menukey) in menuList"
          :key="menukey"
          :class="{ active: active === menukey }"
          @click="scrollTo(menukey)"
        >
          {{ menuItem.name }}
        </li>
      </ul>
    </div>
    <!-- 右侧内容 -->
    <div class="foods-wrapper">
      <div v-for="(foodItem, foodkey) in menuList" :key="foodkey">
        <h2>{{ foodItem.name }}</h2>
        <dl
          v-for="(item, index) in foodItem.foods"
          :key="index"
          @click="details(index)"
        >
          <dd>
            <img :src="item.image" alt="" />
          </dd>
          <dt>
            <p class="name">{{ item.name }}</p>
            <p class="description">{{ item.description }}</p>
            <p class="description">
              月售{{ item.sellCount }}份 好评率{{ item.rating }}%
            </p>
            <div class="price">
              <p><span>￥</span>{{ item.price }}</p>
              <img src="./image/add_circle.png" alt="" />
            </div>
          </dt>
        </dl>
      </div>
    </div>
    <!--底部  -->
    <div id="foot">
      <p class="br-car">
        <img src="./image/car.png" alt="" />
      </p>
      <p class="br-money">
        <span class="price">￥0</span>
        <span>另需配送费￥4元</span>
      </p>
      <button>￥20起送</button>
    </div>
  </div>
</template>


<script>
export default {
  props: [],
  data() {
    return {
      active: 0, // 当前激活的导航索引
      menuList: [],
    };
  },
  methods: {
    // 进入详情
    details(id) {
      this.$router.push("/details?id=" + id);
      console.log(id)
    },
    // 滚动监听器
    // onScroll() {
    //   // 获取所有锚点元素
    //   const navContents = document.querySelectorAll(".foods-wrapper div");
    //   // 所有锚点元素的offsetTop
    //   const offsetTopArr = [];
    //   navContents.forEach((item) => {
    //     offsetTopArr.push(item.offsetTop);
    //   });
    //   // 获取当前文档流的scrollTop
    //   const scrollTop =
    //     document.documentElement.scrollTop + 40 || document.body.scrollTop + 40;
    //   // 定义当前点亮的导航下标
    //   let navIndex = 0;
    //   let count = 0;
    //   let menu = document.getElementById("menu");
    //   for (let n = 0; n < offsetTopArr.length; n++) {
    //     if (scrollTop >= offsetTopArr[n]) {
    //       navIndex = n;
    //       if (navIndex >= 8) count++;
    //     }
    //   }
    //   // 当前active菜单赋值
    //   this.active = navIndex;
    //   // 滚动到一定位置 将菜单整体向上移  确保能看的到更多的菜单
    //   if (navIndex >= 8) {
    //     menu.style.top = -100 * count + "px";
    //   } else {
    //     menu.style.top = 0 + "px";
    //   }
    // },
  },
  mounted() {
    this.$http.get("/data.json").then((res) => {
      console.log(res);
      this.menuList = res.data.goods;
    });
    // 监听滚动事件
    window.addEventListener("scroll", this.onScroll);
  },
  destroy() {
    // 必须移除监听器，不然当该vue组件被销毁了，监听器还在就会出错
    window.removeEventListener("scroll", this.onScroll);
  },
};
</script>


<style scoped>
#commodity {
  position: fixed;
  font-size: 0.3rem;
  display: flex;
  height: 10rem;
  overflow: hidden;
}
.menu-wrapper .menu {
  width: 1.6rem;
  height: 100%;
  font-size: 0.24rem;
  background: rgb(236, 236, 236);
}
.menu-wrapper .menu li {
  height: 1.08rem;
  width: 100%;
  padding: 0.24rem;
  box-sizing: border-box;
}
.foods-wrapper {
  width: 5.8rem;
  height: 100%;
}
h2 {
  width: 80%;
  line-height: 0.52rem;
  background: f3f5f7;
  border-left: 0.04rem solid #d9dde1;
  font-size: 0.23rem;
  color: #93999f;
  padding-left: 0.24rem;
  box-sizing: border-box;
}
dl {
  width: 100%;
  height: 2.18rem;
  padding: 0.36rem;
  box-sizing: border-box;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
dd img {
  width: 1.14rem;
  height: 1.14rem;
}
dt {
  margin-left: 0.2rem;
  width: 4.22rem;
  padding-right: 0.36rem;
  box-sizing: border-box;
}
dt p {
  width: 100%;
}
dt .name {
  font-size: 0.28rem;
  line-height: 0.28rem;
  color: rgb(7, 17, 27);
  font-weight: bold;
  margin-bottom: 0.14rem;
}
dt .description {
  font-size: 0.2rem;
  color: rgb(147, 153, 159);
  margin-bottom: 0.14rem;
}
dt .price {
  font-size: 0.28rem;
  line-height: 0.48rem;
  color: red;
  font-weight: 700;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
dt .price span {
  font-size: 0.2rem;
}
dt .price img {
  width: 0.48rem;
  height: 0.48rem;
}
.br-car {
  position: absolute;
  top: -0.3rem;
  left: 0.23rem;
  width: 1rem;
  height: 1rem;
  background: #2b343c;
  border-radius: 50%;
  border: 0.12rem solid #131d26;
}
.br-car img {
  width: 0.41rem;
  height: 0.4rem;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.br-money {
  width: 5.4rem;
  height: 0.86rem;
  font-size: 0.18rem;
  padding-left: 1.6rem;
  color: #919396;
  text-align: center;
  display: flex;
  align-items: center;
}
.br-money span {
  margin-left: 0.2rem;
  font-size: 0.32rem;
  font-weight: bold;
}
button {
  width: 2.1rem;
  height: 0.96rem;
  background: #2b333b;
  border: none;
  font-size: 0.24rem;
  color: #979a9c;
  font-weight: bold;
}
#foot {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 0.96rem;
  background: #141d27;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
}
</style>